package com.example.hello.controller;


import com.example.hello.authentication.ShiroUtil;
import com.example.hello.entity.User;
import com.example.hello.manager.UserManager;
import com.example.hello.po.UserPo;
import com.example.hello.service.UserService;
import com.example.common.utils.result.ResultBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-07-22
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserManager userManager;

    /**
     * 简单写个登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResultBO login(@RequestParam("username")String username,
                          @RequestParam("password") String password){
        return userManager.login(username, password);
    }

    @GetMapping("/logout")
    public ResultBO logout(){
        return userManager.logout();
    }

    @GetMapping("/list")
    @RequiresRoles("admin")
    public ResultBO listUserPos(){
        User user = ShiroUtil.getUser();
        System.out.println(user);
        return userManager.listUsersInfo();
    }

    @GetMapping("/info")
    public String userInfo(String userName){
        return userManager.getUserByName(userName).toString();
    }
}
